package com.westos.core;

import com.westos.bean.RiskBean;
import com.westos.codeaudit.catchcode.entity.VersionManage;
import com.westos.codeaudit.dispatcher.scan.ScannerConnect;
import com.westos.core.impl.ScanRiskSqlImpl;
import com.westos.core.impl.ScanRiskXssImpl;
import com.westos.gui.Index2;
import com.westos.share.AllTask;
import com.westos.share.ScanType;
import com.westos.utils.GetFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;


public class DoScan extends TimerTask {


    private List<RiskBean> allresult=null;
    private VersionManage versionManage;
    private ScannerConnect scannerConnect;

    public ScannerConnect getScannerConnect() {
        return scannerConnect;
    }

    public void setScannerConnect(ScannerConnect scannerConnect) {
        this.scannerConnect = scannerConnect;
    }

    public DoScan(VersionManage versionManage) {
        this.versionManage = versionManage;
    }

    public void run() {
        if (!AllTask.taskList.isEmpty()) {
            VersionManage versionManage = AllTask.taskList.get(0);
            List<RiskBean> list = scanTask(versionManage);
            System.out.println("****************************************************************************");
            System.out.println("*任务信息："+versionManage);
            System.out.println("*任务结果："+list);
            System.out.println("****************************************************************************");
            scannerConnect.finishScan(list,versionManage);//调用调度器的finish方法，将结果给他
            //new Index2(list);//图形化界面显示

    }

    }

    public void doScan() {
        AllTask.taskList.add(this.versionManage);//向任务队列中添加次任务
        //System.out.println("当前队列中的任务："+AllTask.taskList);
    }

    public List scanTask(VersionManage versionManage) {
        for (VersionManage bean : AllTask.taskList) {
            if (bean.equals(versionManage)) {
                Integer taskid = versionManage.getVerid();
                String dir = versionManage.getPropath();
                String kind = versionManage.getScanway();
                File dir1 = new File(dir);
                GetFile getFile = new GetFile();//new新的，因为如果有两个任务，会把它加再同一个list里。
                List<File> files = getFile.getFile(dir1);
                if (kind.equals(ScanType.SQL)) {
                    allresult = new ArrayList();
                    System.out.println("任务进入了sql注入扫描---" + "id:" + taskid);
                    for (File file : files) {
                        ScanRiskSqlImpl scanRiskSql = new ScanRiskSqlImpl();
                        //System.out.println(file.getParent()+ file.getName());
                        List<RiskBean> scansqllist = scanRiskSql.scan(file, taskid);
                        for (RiskBean riskBean : scansqllist) {
                            allresult.add(riskBean);
                        }

                    }
                } else if (kind.equals(ScanType.XSS)) {
                    allresult = new ArrayList();
                    System.out.println("任务进入了Xss注入扫描---" + "id:" + taskid);
                    for (File file : files) {
                        ScanRiskXssImpl scanRiskXss = new ScanRiskXssImpl();
                        //System.out.println(file.getParent()+ file.getName());
                        List<RiskBean> scanxsslist = scanRiskXss.scan(file, taskid);
                        for (RiskBean riskBean : scanxsslist) {
                            allresult.add(riskBean);
                        }
                    }

                } else if (kind.equals(ScanType.SQLandXSS)) {
                    allresult = new ArrayList();
                    System.out.println("任务进入了sql和Xss注入扫描---" + "id:" + taskid);
                    for (File file : files) {
                        ScanRiskSqlImpl scanRiskSql = new ScanRiskSqlImpl();
                        //System.out.println(file.getParent()+ file.getName());
                        List<RiskBean> scansqllist = scanRiskSql.scan(file, taskid);
                        for (RiskBean riskBean : scansqllist) {
                            allresult.add(riskBean);
                        }
                    }
                    for (File file : files) {
                        ScanRiskXssImpl scanRiskXss = new ScanRiskXssImpl();
                        //System.out.println(file.getParent()+ file.getName());
                        List<RiskBean> scanxsslist = scanRiskXss.scan(file, taskid);
                        for (RiskBean riskBean : scanxsslist) {
                            allresult.add(riskBean);
                        }
                    }
                }
                //扫描完成后从创建任务的list队列中删除
                AllTask.taskList.remove(versionManage);
            }
        }

        return allresult;

    }


}


